package marcus.spring.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import marcus.spring.form.AgeModel;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes
public class AgeCaclulatorController {

	@InitBinder
	private void dateBinder(WebDataBinder binder) {
	            //The date format to parse or output your dates
	    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	            //Create a new CustomDateEditor
	    CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
	            //Register it as custom editor for the Date type
	    binder.registerCustomEditor(Date.class, editor);
	}
	
	@RequestMapping(value = "/date")
	public ModelAndView getAges(@ModelAttribute("age") AgeModel age, BindingResult result)
	{
		if (age.getDate() != null){
			//TODO Remove Nia hardcoding
			age.setUsername("Nia");
			System.out.println("Date: " + age.getDate());
			System.out.println("User: " + age.getUsername());
			age.setContactAges();
		}
		
		return new ModelAndView("date","command", age);
	}

}
